class StripsController < ApplicationController
  
  before_filter :login_required
  before_filter :check_user, :only => [:edit, :update, :destroy, :index]
  
  # GET /strips
  # GET /strips.xml
  def index
    @strips = Strip.find(:all, :conditions => {:webcomic_id => params[:webcomic_id]}, :order => "strip_number DESC")
  end

  # GET /strips/1
  # GET /strips/1.xml
  def show
    @strip = Strip.find(params[:id])
    @webcomic = Webcomic.find(params[:webcomic_id])
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @strip }
    end
  end

  # GET /strips/new
  # GET /strips/new.xml
  def new
    @webcomic = Webcomic.find(params[:webcomic_id])
    @strip = Strip.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @strip }
    end
  end

  # GET /strips/1/edit
  def edit
    @webcomic ||= Webcomic.find(params[:webcomic_id])
    @strip ||= Strip.find(params[:id])
  end

  # POST /strips
  # POST /strips.xml
  def create
    @webcomic = Webcomic.find(params[:webcomic_id])
    @strip = Strip.new(params[:id])
    @strip.webcomic = @webcomic
    @strip.strip_number = Strip.getNextStripNumber(@strip.webcomic.id)
    
    respond_to do |format|
      if @strip.save
        flash[:notice] = 'Strip was successfully created.'
        format.html { redirect_to(webcomic_strips_path(@webcomic)) }
        format.xml  { render :xml => @strip, :status => :created, :location => @strip }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @strip.errors, :status => :unprocessable_entity }
      end
    end

  end

  # PUT /strips/1
  # PUT /strips/1.xml
  def update
    @webcomic ||= Webcomic.find(params[:webcomic_id])
    @strip ||= Strip.find(params[:id])
    
    respond_to do |format|
      if @strip.update_attributes(params[:strip])
        flash[:notice] = 'Strip was successfully updated.'
        format.html { redirect_to(webcomic_strip_url(@webcomic,@strip)) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @strip.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /strips/1
  # DELETE /strips/1.xml
  def destroy
    @webcomic ||= Webcomic.find(params[:webcomic_id])
    @strip ||= Strip.find(params[:id])

    @destroyed_strip_number = -1
    if @strip
      destroyed_strip_number = @strip.strip_number
    end
    @strip.destroy

    if @destroyed_strip_number >= 0
      laterStrips = Strip.find(:all, :conditions => ["webcomic_id = #{params[:webcomic_id]} AND strip_number > #{@strip.strip_number}"])      
      laterStrips.each do |aStrip|
        aStrip.strip_number = aStrip.strip_number - 1
        aStrip.save
      end
    end

    respond_to do |format|
      format.html { redirect_to(webcomic_strips_path(@webcomic)) }
      format.xml  { head :ok }
    end
  end
  
  private
  
    def required_login
    redirect_to :controller => :session, :action => :new and return unless logged_in?
  end
    
  def check_user
    @webcomic = Webcomic.find(params[:webcomic_id])
    unless @webcomic.creator_id == current_user.id
      access_denied
    end
  end
  
  def access_denied
    if logged_in?
      render :text => "You have insufficient permissions", :status => 401
      return false
    else
      super
    end
  end

end
